﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Serialization;
using UnityEngine.UI;

public enum GuideType
{
    Rect,
    Circle
}

[Serializable]
public class GuideData
{
    public GuideType Type;
    public RectTransform RectTransform;
    public string desc;
}

public class GuidePanel : MonoBehaviour
{
    public GameObject _rectGuidePrefab;
    public GameObject _circleGuidePrefab;
    public Canvas _canvas;
    public List<GuideData> _datas = new List<GuideData>();

    private List<GuideBase> _guides = new List<GuideBase>();

    private Action _done;

    private void Start()
    {
        for (int i = 0; i < _datas.Count; i++)
        {
            GameObject go = null;

            switch (_datas[i].Type)
            {
                case GuideType.Circle:
                    go = Instantiate(_circleGuidePrefab, transform);
                    var guideCircle = go.GetComponent<CircleGuide>();
                    guideCircle.Init(i, _canvas, _datas[i].RectTransform, Callback);
                    _guides.Add(guideCircle);
                    break;
                case GuideType.Rect:
                    go = Instantiate(_rectGuidePrefab, transform);
                    var guideRect = go.GetComponent<RectGuide>();
                    guideRect.Init(i, _canvas, _datas[i].RectTransform, Callback);
                    _guides.Add(guideRect);
                    break;
            }

            go?.SetActive(false);
        }
    }

    public void ShowGuide(Action done)
    {
        _done = done;
        Callback(-1);
    }

    private void Callback(int index)
    {
        if (index + 1 < _guides.Count)
        {
            index++;
            for (int i = 0; i < _guides.Count; i++)
            {
                _guides[i].gameObject.SetActive(i == index);
                if (i == index)
                {
                    if (_guides[i].name.Contains("Rect"))
                    {
                        var a = (RectGuide)_guides[i];
                        a.Show(_datas[i].desc);
                    }

                    if (_guides[i].name.Contains("Circle"))
                    {
                        var a = (CircleGuide)_guides[i];
                        a.Show(_datas[i].desc);
                    }
                }
            }
        }
        else
        {
            _done?.Invoke();
            for (int i = 0; i < _guides.Count; i++)
            {
                _guides[i].gameObject.SetActive(false);
            }
        }
    }
}